package cn.starrys.tool.mail

import cn.starrys.tool.mail.entity.Mail
import cn.starrys.tool.mail.entity.MailProps
import java.io.File
import java.util.*
import kotlin.test.BeforeTest
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertTrue

class MailToolsTest() {
    private lateinit var props: Properties

    @BeforeTest
    fun init() {
        val properties = Properties()
        val inputStream = javaClass.classLoader.getResourceAsStream("mail.properties")
        properties.load(inputStream)
        props = properties
    }

    @Test
    // @Ignore("依据需求")
    fun sendTest() {
        val from = props.getProperty("from")
        val password = props.getProperty("password")
        val addressee = props.getProperty("addressee")

        // 邮件配置
        val mps = MailProps("smtp.163.com", 465, from, password, "starry tool", true, true, Charsets.UTF_8, true)

        // 创建邮件工具
        val mailTools = MailTools(mps)

        // 创建一封邮件内容
        val mail = Mail(
            ArrayList<Mail.Addressee>().apply { add(Mail.Addressee(addressee, "Q(收件人)")) },
            ArrayList<Mail.Addressee>().apply { add(Mail.Addressee(addressee, "XK(抄送人)")) },
            ArrayList<Mail.Addressee>().apply { add(Mail.Addressee(addressee, "M(密送人)")) },
            Mail.Type.HTML,
            "星空验证码",
            "您的验证码是<b style='font-size: 20px'><code style='color: deepskyblue'>${(1000..9999).random()}</code></b>",
            ArrayList<File>().apply { add(File("src/test/resources/mail.properties")) }
        )

        // 发送邮件
        val sendStatus = mailTools.send(mail)

        // 断言发送成功
        assertTrue(sendStatus)
    }
}
